package com.gba.mark.labelResult.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.group.entity.Group;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.labelResult.entity.LabelResult;
import com.gba.mark.labelResult.entity.dto.LabelResultDTO;
import com.gba.mark.labelResult.mapper.LabelResultMapper;
import com.gba.mark.labelResult.service.ILabelResultService;

import com.gba.mark.origin.entity.Origin;
import com.gba.mark.origin.entity.OriginStatus;
import com.gba.mark.origin.mapper.OriginMapper;
import com.gba.mark.origin.service.IOriginService;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.service.user.IUserService;
import com.hankcs.hanlp.classification.features.IFeatureWeighter;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@Service
public class LabelResultServiceImpl extends ServiceImpl<LabelResultMapper, LabelResult> implements ILabelResultService {
    @Autowired
    private IGroupService groupService;

    @Autowired
    private IUserService userService;

    @Autowired
    private OriginMapper originMapper;

    @Autowired
    private IOriginService originService;

    @Autowired
    private LabelResultMapper labelResultMapper;

    @Autowired
    private ILabelResultService labelResultService;

    @Override
    public JSONObject count(String username) {
        JSONObject counts = new JSONObject();
        List<Group> groupList = groupService.listGroup(username);
        for (Group g : groupList) {
            JSONObject groupCount = new JSONObject();
            int groupId = g.getId();
            groupCount.put("group_name", g.getName());
            groupCount.put("language", g.getLanguage());
            int userId = userService.getOne(new QueryWrapper<User>().eq("username", username)).getId();
            groupCount.put("count", getCountByGroup(userId, groupId));
            counts.put(String.valueOf(groupId), groupCount);
        }
        return counts;
    }

    public JSONObject getCountByGroup(int userId, int groupId) {
        JSONObject count = new JSONObject();
        count.put("daily", this.baseMapper.countDaily(userId, groupId));
        count.put("week", this.baseMapper.countWeek(userId, groupId));
        count.put("total", this.baseMapper.countTotal(userId, groupId));
        return count;
    }


    @Override
    public Boolean proofreading(int originId, String newResults, int status) {//交叉验证
        //第一次标注
        if(status == OriginStatus.UNLABELED.getData()){
            return true;
        }else {
            //取出对应的的标注结果
            List<LabelResult> User_list =  labelResultMapper.selectList(new QueryWrapper<LabelResult>().eq("origin_id", originId));
            for (LabelResult list : User_list){
                String str1 = list.getResult().trim();
                String str2 = newResults.trim();

                //如果交叉验证成功或第一次标注
                if( str1.equals(str2)){
                   continue;
                }else {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public boolean submit(LabelResultDTO labelResultDTO, int userId){
        int originId = labelResultDTO.getOriginId();
        String newResult = labelResultDTO.getResult();
        Origin origin = originService.getById(originId);
        int status = originMapper.selectOne(new QueryWrapper<Origin>().eq("id", originId)
                .eq("active",true)).getStatus();

        if(proofreading(originId, newResult, status) ){//标注成功的或第一次标注
            //三元运算符 判断status是否为19
    //            status = status < 20  ?  status + 1 : return false;
            if(status < OriginStatus.MARK_MAX.getData()){
                status = status +1;
                origin.setStatus(status);
            }else {
                return false;
            }

        }else {//标注不成功
            origin.setStatus(OriginStatus.MARK_FALSE.getData());

        }

        if(originService.updateById(origin)){
            System.out.println("\n" + new Date() + ": origin_id = "+originId+" ,status update success!");

            try {
                //获取当前时间
                Date date1 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date2 = sdf.format(date1);
                //保存进到表中
                LabelResult labelResult = new LabelResult();
                labelResult.setResult(newResult);
                labelResult.setOriginId(originId);
                labelResult.setUserId(userId);
                labelResult.setPost_date(sdf.parse(date2));
                labelResultService.save(labelResult);

            } catch (Exception e) {
                System.out.println("\n" + new Date() + ": origin_id = "+originId+" ,save failed...");
            }
        } else {
            System.out.println("\n" + new Date() + ": origin_id = "+originId+" ,status update failed... ");
        }

        //解锁
        if(originService.save(origin)){
            originMapper.unLockOrigin(originId);
        }else {
            return false;
        }
        return true;
    }
}